package cn.wolfcode.web.controller;

import cn.wolfcode.qo.JsonResult;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {

    @RequestMapping("login")
    @ResponseBody
    public JsonResult login(String username, String password){
        try {
            //获取Subject主体
            Subject subject = SecurityUtils.getSubject();
            //把前端传过来的参数封装为令牌
            UsernamePasswordToken token = new UsernamePasswordToken(username,password);
            //使用shiro提供的api来登录
            subject.login(token);
            return new JsonResult();
        } catch (UnknownAccountException e) {
            e.printStackTrace();
            return new JsonResult(false,"用户名不存在");
        }catch (IncorrectCredentialsException e){
            e.printStackTrace();
            return new JsonResult(false,"密码错误");
        }catch (DisabledAccountException e){
            e.printStackTrace();;
            return new JsonResult(false,"账号被禁用");
        }
    }

}
